package buildings;

import buildings.exceptions.InexchangeableFloorsException;
import buildings.exceptions.InexchangeableSpacesException;

public class PlacementExchanger {
    
    private static boolean isSpacesExchangeable(Space space1, Space space2) {
        return space1.getSquare() == space2.getSquare() &&
                space1.getRoomCount() == space2.getRoomCount();
    }
    
    private static boolean isFloorsExchangeable(Floor floor1, Floor floor2) {
        return floor1.getTotalSquare() == floor2.getTotalSquare() &&
                floor1.getTotalRoomCount() == floor2.getTotalRoomCount();
    }
    
    public static void exchangeFloorRooms(Floor floor1, int index1, 
            Floor floor2, int index2) throws InexchangeableSpacesException {
        if(!isSpacesExchangeable(floor1.getSpaceByNumber(index1), 
                floor2.getSpaceByNumber(index2))) {
            throw new InexchangeableSpacesException();
        }
        Space buf = floor1.getSpaceByNumber(index1);
        floor1.setSpaceByNumber(index1, floor2.getSpaceByNumber(index2));
        floor2.setSpaceByNumber(index2, buf);
    }
    
    public static void exchangeBuildingFloors(Building building1, 
            int index1, Building building2, int index2) 
            throws InexchangeableFloorsException {
        if(!isFloorsExchangeable(building1.getFloorByNumber(index1), 
                building1.getFloorByNumber(index2))) {
            throw new InexchangeableFloorsException();
        }
        Floor buf = building1.getFloorByNumber(index1);
        building1.setFloorByNumber(index1, building2.getFloorByNumber(index2));
        building2.setFloorByNumber(index2, buf);
    }
}
